<?php

namespace app\shop\job;

use app\shop\exception\ParamsErrorException;
use app\shop\service\OrderService;
use think\queue\Job;
use Throwable;

class AutoCancelOrderJob extends BaseJob
{
    public static string $queue = 'auto_cancel_order';

    public function fire(Job $job, $data): void
    {
        $this->start($job, $data);

        $orderId = $data['order_id'] ?? 0;
        if (!$orderId) {
            $this->noNeedJob($job, ['data' => $data, 'msg' => '订单ID不存在']);
            return;
        }

        $orderService = new OrderService();
        try {
            $orderService->autoCancel($orderId);
            $this->successJob($job);
            return;
        } catch (ParamsErrorException $e) {
            $this->noNeedJob($job, ['msg' => $e->getMessage()]);
            return;
        } catch (Throwable $e) {
            $this->exceptionJob($job, ['data' => $data, 'err' => $e->getMessage()], $e);
        }
    }
}